function [recs, err] = record_parse(list, num)
% parse the list of the format:
% 12;23/10 etc... like nxtrec in datac
%
% first arg is starting number for output array
% second arg is final number
% / indicates the skip arg type
% basic: /n means skip n : e.g., 1;10/2 = 1,3,5,7,9
% 
% num present implies that only the nth (num) set of entries
% in the list will be parsed.
%
err=0;
recs=[];
   
list=deblank(list); % remove extraneous stuff...
% now break the list into tokens and parse each token
nl = 0;
while(any(list))
	[plist, list] = strtok(list,',');
   if(nargin > 1)
      nl = nl + 1;
      if(nl == num)
         recs = recparse(plist);
         return;
      end;
   else
	   recs = [recs recparse(plist)];
   end;
end
return;	


function [recs] = recparse(list)
% function to parse basic word unit of the list - a;b/c or the like
%
recs=[];
if(isempty(list))
   return;
end

fn=[]; ln=[]; sn=[]; arg=[];
[fn, rest] = strtok(list, ';');
[ln, rest]=strtok(rest, ';/');
[sn, rest] = strtok(rest,'/');
u=isletter(sn);
if(any(u)) % must have another kind of skip argument
	arg=sn(u==1);
	sn=sn(u==0);	% just the number there...
end;

if(~isempty(rest))
	display(sprintf('recparse: Bad Record list to parse: %s', list))
	return;
end
if(~isempty(fn) & isempty(str2num(fn)))
	display(sprintf('recparse: Bad first arg: "%s" in "%s"', fn, list));
	return;
end
if(~isempty(ln) & isempty(str2num(ln)))
	display(sprintf('recparse: Bad last arg: "%s" in "%s"', ln, list));
	return;
end
if(~isempty(sn) & isempty(str2num(sn)))
	display(sprintf('recparse: Bad skip arg: "%s" in "%s"', sn, list));
	return;
end
	
if(isempty(sn) & ~isempty(fn) & ~isempty(ln))
   recs=str2num(char(fn)):str2num(char(ln));
   return;
end
if(~isempty(fn) & ~isempty(ln) & ~isempty(sn) & isempty(arg))
   recs=str2num(char(fn)):str2num(char(sn)):str2num(char(ln));
   return;
end
if(~isempty(fn) & ~isempty(ln) & ~isempty(sn) & ~isempty(arg))
   a=str2num(char(fn));
   b=str2num(char(ln));
   n=str2num(char(sn));
   switch(arg)
   case 'n' % just n steps between a and b
   		sk=(b-a)/(n-1);	
   		recs=a:sk:b;
	case 'r' % n steps between a and b, but randomize order.. 
   		sk=(b-a)/(n-1);	
   		recs=a:sk:b;
		v=randperm(length(recs)); % get random order array to use here
		recs=recs(v); % reorder result!
	case 'l' % make log steps between the elements.
		la = log(a);
		lb = log(b);
		ls = (lb-la)/(n-1);
		lrecs=la:ls:lb;
		recs=exp(lrecs);		
	case 's' % make log steps, but then randomize them
		la = log(a);
		lb = log(b);
		ls = (lb-la)/(n-1);
		lrecs=la:ls:lb;
		recs=exp(lrecs);		
		v=randperm(length(recs)); % get random order array to use here
		recs=recs(v); % reorder result!
	otherwise
	end;
   return;
end

if(isempty(sn) & isempty(ln) & ~isempty(fn))
	recs=str2num(char(fn));
	return;
end
	
display(sprintf('recparse: Can''t parse: %s', list))
return;

      

